package com.jxd.ordersys.controller;

import com.jxd.ordersys.dao.UserMapper;
import com.jxd.ordersys.model.User;
import com.jxd.ordersys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author 王云超
 * @Date 2024/9/19 17:31
 * @Version 1.0
 */
@RestController
@SessionAttributes({"username","role"})
public class LoginController {
    @Autowired
    IUserService userService;

    @RequestMapping("/login")
    public Map<String,Object> login(Model model, @RequestBody User users){
        String username = users.getUsername();
        String password = users.getPassword();

        Map<String,Object> user = userService.getUser(username,password);
        //如果查询结果不为空,登录成功,将员工的信息存入session中
        Map<String, Object> response = new HashMap<>();
        if (user!=null){
            // 准备返回的数据
            String role = (String) user.get("role");
            String empName = (String) user.get("empName");
            response.put("msg", "success");
            response.put("username", username); // 假设这是从user表中获取的
            response.put("role", role); // 假设这是从emp表中获取的
            response.put("empName", empName); // 假设这是从emp表中获取的
        }else {
            response.put("msg", "error");
        }
        return response;
    }
}